<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="./commons/common-css.html :: commonHead('图书封面图片')">
	<meta charset="UTF-8"/>
</head>
<body>
<div id="uploader-demo" style="text-align: center;">
    <!--用来存放item-->
    <div id="fileList" class="uploader-list"></div>
    <div id="filePicker">选择图片</div>
</div>
<span th:include="./commons/common-js.html :: commonOnLoadJs"></span>
</body>
<script type="text/javascript">
const BASE_PATH = [[@{/}]], id = [[${id}]];;
var uploader = WebUploader.create({
	auto: true,
	swf: BASE_PATH + '/static/component/webuploader/Uploader.swf',
	// 文件接收服务端。
	server: BASE_PATH + '/restful/book/uploadBookImage/'+id,
	pick: '#filePicker',
	accept: {
        title: 'Images'
    }
})
var $list = $('#fileList');
uploader.on('fileQueued', function(file) {
    var $li = $('<div id="' + file.id + '" class="file-item thumbnail">' +
                '<img>' +
                '<div class="info">' + file.name + '</div>' +
            '</div>'
            ), $img = $li.find('img');
    $list.append($li);
    uploader.makeThumb(file, function(error, src) {
        if (error) {
            $img.replaceWith('<span>不能预览</span>');
            return;
        }
        $img.attr('src', src);
    });
});

uploader.on('uploadProgress', function(file, percentage) {
    var $li = $('#'+file.id),
        $percent = $li.find('.progress span');
    // 避免重复创建
    if (!$percent.length) {
        $percent = $('<p class="progress"><span></span></p>')
                .appendTo($li)
                .find('span');
    }
    $percent.css('width', percentage * 100 + '%');
});

// 文件上传成功，给item添加成功class, 用样式标记上传成功。
uploader.on('uploadSuccess', function(file) {
    $('#'+file.id).addClass('upload-state-done');
});

// 文件上传失败，显示上传出错。
uploader.on('uploadError', function(file) {
    var $li = $('#'+file.id),
        $error = $li.find('div.error');
    // 避免重复创建
    if (!$error.length) {
        $error = $('<div class="error"></div>').appendTo($li);
    }
    $error.text('上传失败');
});

// 完成上传完了，成功或者失败，先删除进度条。
uploader.on('uploadComplete', function(file) {
    $('#'+file.id).find('.progress').remove();
    layer.alert('图片上传成功!', function(i) {
    	layer.close(i);
    	var index = parent.layer.getFrameIndex(window.name);
    	parent.layer.close(index);
    });
});
</script>
</html>